extends "res://BaseEnemy.gd"

# 冰元素特有属性
@export var ice_shard_cooldown = 3.0  # 冰碎片冷却时间
@export var ice_shard_damage = 10  # 冰碎片伤害
@export var ice_shard_count = 3  # 一次发射的冰碎片数量
@export var ice_shard_spread = 30.0  # 冰碎片扩散角度（度）
@export var freeze_aura_radius = 100.0  # 冻结光环半径
@export var freeze_aura_slow_factor = 0.8  # 冻结光环减速效果
@export var freeze_chance = 0.1  # 冻结几率

var ice_shard_timer = 0.0
var can_shoot_ice_shard = true
var aura_pulse_timer = 0.0

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置冰元素特有属性
	max_health = 45
	current_health = max_health
	movement_speed = 80.0
	damage = 8
	experience_value = 9
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.7, 0.9, 1.0)  # 冰蓝色调

func _physics_process(delta):
	# 调用父类的_physics_process方法
	super._physics_process(delta)
	
	# 处理冰碎片冷却
	if not can_shoot_ice_shard:
		ice_shard_timer += delta
		if ice_shard_timer >= ice_shard_cooldown:
			can_shoot_ice_shard = true
			ice_shard_timer = 0.0
	
	# 冻结光环脉冲
	aura_pulse_timer += delta
	if aura_pulse_timer >= 1.0:  # 每秒检查一次
		aura_pulse_timer = 0.0
		apply_freeze_aura()
	
	# 如果可以发射冰碎片且玩家存在，尝试发射冰碎片
	if can_shoot_ice_shard and target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= 200 and distance > 50:  # 在适当距离时发射冰碎片
			shoot_ice_shards()

func shoot_ice_shards():
	if animation_player and animation_player.has_animation("attack"):
		animation_player.play("attack")
	
	# 计算基础方向（朝向玩家）
	var base_direction = (target_player.global_position - global_position).normalized()
	
	# 发射多个冰碎片
	for i in range(ice_shard_count):
		# 计算扩散角度
		var angle_offset = 0.0
		if ice_shard_count > 1:
			angle_offset = deg_to_rad(ice_shard_spread) * (i / float(ice_shard_count - 1) - 0.5) * 2.0
		
		# 旋转基础方向
		var direction = base_direction.rotated(angle_offset)
		
		# 创建冰碎片投射物
		var ice_shard = preload("res://IceShardProjectile.tscn").instantiate()
		get_tree().get_root().add_child(ice_shard)
		ice_shard.global_position = global_position
		
		# 设置冰碎片属性
		ice_shard.direction = direction
		ice_shard.speed = 250.0
		ice_shard.damage = ice_shard_damage
		ice_shard.freeze_chance = freeze_chance
		ice_shard.source = self
	
	# 重置冰碎片冷却
	can_shoot_ice_shard = false
	ice_shard_timer = 0.0
	
	print("冰元素发射冰碎片!")

func apply_freeze_aura():
	# 检查玩家是否在冻结光环范围内
	if target_player and is_instance_valid(target_player):
		var distance = global_position.distance_to(target_player.global_position)
		if distance <= freeze_aura_radius:
			# 应用减速效果
			if target_player.has_method("apply_slow_effect"):
				target_player.apply_slow_effect(freeze_aura_slow_factor, 1.5)  # 持续1.5秒
			
			# 尝试冻结
			if randf() < freeze_chance:
				if target_player.has_method("apply_freeze_effect"):
					target_player.apply_freeze_effect(1.0)  # 冻结1秒
			
			# 创建冻结光环视觉效果
			var aura_effect = preload("res://FreezeAuraEffect.tscn").instantiate()
			get_tree().get_root().add_child(aura_effect)
			aura_effect.global_position = global_position
			aura_effect.radius = freeze_aura_radius

# 重写受伤方法，有几率在受伤时发射冰碎片
func take_damage(amount):
	super.take_damage(amount)
	
	# 25%几率在受伤时发射冰碎片
	if randf() < 0.25 and target_player and is_instance_valid(target_player):
		shoot_ice_shards()

# 重写死亡方法，死亡时爆发冰碎片
func die():
	# 死亡时向四面八方发射冰碎片
	for i in range(8):
		var angle = TAU * i / 8.0
		var direction = Vector2(cos(angle), sin(angle))
		
		# 创建冰碎片投射物
		var ice_shard = preload("res://IceShardProjectile.tscn").instantiate()
		get_tree().get_root().add_child(ice_shard)
		ice_shard.global_position = global_position
		
		# 设置冰碎片属性
		ice_shard.direction = direction
		ice_shard.speed = 200.0
		ice_shard.damage = ice_shard_damage
		ice_shard.freeze_chance = freeze_chance
	
	# 调用父类的die方法
	super.die()
